import time


class Performance:
    def method(self):
        return "result"


obj = Performance()

# 测试直接方法调用与反射方法调用的性能差异

# 直接方法调用
start = time.time()
for _ in range(100000):
    obj.method()  # 直接方法调用
direct_time = time.time() - start


# 通过getattr反射方法调用
start = time.time()
for _ in range(100000):
    getattr(obj, "method")()  # 反射方法调用
reflect_time = time.time() - start

print(f"直接调用: {direct_time:.6f}s")
print(f"反射调用: {reflect_time:.6f}s")
print(f"性能差异: {reflect_time/direct_time:.2f}x")
